<?php  

class Pm extends Controller{
	
	function __construct(){
		parent::Controller();
		$this->load->model('login_model');
		$this->load->model('pm_model');
	}
	
	function index(){
		//Userdata
		$gebruiker['users_naam'] = $this->session->userdata('username');
		$user_id = $this->login_model->getUserID($gebruiker);
		$ingelogd = $this->login_model->is_ingelogd(); 
		
		//Variabelen
		$data['titel'] = "Berichten Overzicht";
		$data['error'] = "";
		$data['query'] = $this->pm_model->getPM($user_id);
		
		if($ingelogd){
			$this->load->view('pm_overzicht_view',$data);
		}
		else{
			redirect('login');
		}
	}
	
	function nieuwBericht(){
		$data['titel'] = "Nieuw Bericht Maken";
		$data['error'] = "";
		
		$gebruiker['users_naam'] = $this->session->userdata('username');
		$user_id = $this->login_model->getUserID($gebruiker);
		$ingelogd = $this->login_model->is_ingelogd(); 
		
		if($ingelogd){
			$this->load->view('pm_nieuw_view',$data);
		}
		else{
			redirect('login');
		}
	}
	
	function verstuurPM(){
		$data['error'] = "";
		$data['titel'] = "Nieuw Bericht Maken";
		
		$gebruiker['users_naam'] = $this->session->userdata('username');
		$user_id = $this->login_model->getUserID($gebruiker);
		$ontvangerbestaat = $this->pm_model->ontvangerBestaat($this->input->post('ontvanger'));
		
		//Kijk of alles is ingevuld
		$this->form_validation->set_rules('ontvanger', 'ontvanger', 'trim|required');
		$this->form_validation->set_rules('onderwerp', 'onderwerp', 'trim|required');
		$this->form_validation->set_rules('bericht', 'bericht', 'trim|required');
		
		//Haal info op die ingevuld is
		$pminfo['pm_verzender'] = $user_id;
		$pminfo['pm_onderwerp'] = $this->input->post('onderwerp');
		$pminfo['pm_bericht'] = $this->input->post('bericht');
		$pminfo['pm_tijd'] = time();
		
		// Kijk of er wel een ID is
		if($ontvangerbestaat == 1){
			$pminfo['pm_ontvanger'] = $this->pm_model->zoekID($this->input->post('ontvanger'));
		}
		else{
			echo "";
		}
		
		//Kijk of alles is ingevuld en verstuur de gegevens
		if ($this->form_validation->run() == TRUE){
			if($ontvangerbestaat == 1){
				$this->pm_model->verstuurPM($pminfo);
				$this->index();
			}
			else{
				$data['error'] = "Ontvanger bestaat niet";
				$this->load->view('pm_nieuw_view',$data);
			}
		}
		else{
			$this->nieuwBericht();
		}
	}
	
	function bekijk(){
		$gebruiker['users_naam'] = $this->session->userdata('username');
		$user_id = $this->login_model->getUserID($gebruiker);
		$ingelogd = $this->login_model->is_ingelogd(); 
				
		$pminfo['pm_id'] = $this->uri->segment(3);
		$pminfo['pm_ontvanger'] = $user_id;
		$pmbestaat = $this->pm_model->bestaatPM($pminfo['pm_id']);
		
		$data['query'] = $this->pm_model->getPMInfo($pminfo);
		$data['error'] = "";
		
		if($ingelogd){
			if($pmbestaat == 1){
				$this->pm_model->setGelezen($pminfo['pm_id']);
				$this->load->view('pm_view',$data);
			}
			else{
				redirect('pm');
			}
		}
		else{
			redirect('login');
		}
	}
	
	function verwijder(){
		$gebruiker['users_naam'] = $this->session->userdata('username');
		$user_id = $this->login_model->getUserID($gebruiker);
		$ingelogd = $this->login_model->is_ingelogd(); 
		
		$pm_id = $this->uri->segment(3);
		
		if($ingelogd){
			$this->pm_model->verwijderPM($pm_id);
			$this->index();
		}
		else{
			redirect('login');		
		}
	}
	
	function verzonden(){
		//Userdata
		$gebruiker['users_naam'] = $this->session->userdata('username');
		$user_id = $this->login_model->getUserID($gebruiker);
		$ingelogd = $this->login_model->is_ingelogd(); 
		
		//Variabelen
		$data['titel'] = "Berichten Overzicht";
		$data['error'] = "";
		$data['query'] = $this->pm_model->getPMVerzonden($user_id);
		
		if($ingelogd){
			$this->load->view('pm_overzicht_verzonden',$data);
		}
		else{
			redirect('login');
		}
	}
}


?>